package test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.lex.awt.Location;
import org.lex.swing.ActionBuilder;

import lex.BuildinDecorations;
import lex.WindowDecoration;
import lex.util.ImageCache;

public class Application extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel pane = null;
	// ====================================
	private JMenuBar mainMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu helpMenu = null;
	// =====================================
	private JScrollPane scrollPane = null;
	private JTextArea textArea = null;

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				this.setupLookAndFeel();
				// =========================
				Application app = new Application();
				app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				Location.moveToScreenCenter(app);
				app.setVisible(true);
			}

			private void setupLookAndFeel() {
				try {
					WindowDecoration d = new WindowDecoration();
					d.setDecoration(BuildinDecorations.MAXTHON);
					UIManager.setLookAndFeel(d);
				} catch (Exception e) {
				}
			}
		});
	}

	public Application() {
		super();
		initialize();
	}

	private void initialize() {
		this.setSize(300, 200);
		this.setJMenuBar(getMainMenuBar());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getPane());
		this.setTitle("Application");
		this.setIconImage(ImageCache.getInstance().getImage("/icons/x.png"));
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(getScrollPane(), BorderLayout.CENTER);
		}
		return pane;
	}

	private JMenuBar getMainMenuBar() {
		if (mainMenuBar == null) {
			mainMenuBar = new JMenuBar();
			mainMenuBar.add(getFileMenu());
			mainMenuBar.add(getHelpMenu());
		}
		return mainMenuBar;
	}

	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu("File");
			fileMenu.add(new ActionBuilder("Open...") {
				public void doInBackground(ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					fc.showOpenDialog(Application.this);
				}
			});
			fileMenu.addSeparator();
			fileMenu.add(new ActionBuilder("Exit") {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return fileMenu;
	}

	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu("Help");
			helpMenu.add(new ActionBuilder("About...") {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(Application.this, "Message.", "About...", JOptionPane.INFORMATION_MESSAGE);
				}
			});
		}
		return helpMenu;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}
}
